clear all;

%% construct data

cd ..; cd ..; cd ..;
load('code_main_model\6_implied_w_r\computation_results\data.mat','d','nmkt','Pi','Z','pdf_A_ini','w_guess','r_guess','period_trans','nyears','nz','nb_pdf','ntheta_pdf');
load('code_main_model\6_implied_w_r\input\comm_loc_model.mat','comm_loc_model','bank_opt');
cd 'code_figure_table\OA\Table OA12';

r_end_index=find(r_guess(:,1)==0,1,'first')-1;
w_end_index=find(squeeze(w_guess(r_end_index,:,1,1))==0,1,'first')-1;

r=squeeze(r_guess(r_end_index,:));
w=squeeze(w_guess(r_end_index,w_end_index,:,:));

load('input.mat','distance');

%% write Pi
fileID = fopen('simulation_input\Pi.dat','w');
print1(fileID,Pi,nmkt);

%% write Z
fileID = fopen('simulation_input\Z.dat','w');
print1(fileID,Z,nmkt);

%% write w
fileID = fopen('simulation_input\w.dat','w');
print2(fileID,w,period_trans,nmkt);

%% write r
fileID = fopen('simulation_input\r.dat','w');
print1(fileID,r,period_trans);

%% write comm_loc_model
comm_loc_model(:,nyears+1:period_trans)=0;
for t=nyears+1:period_trans
    comm_loc_model(:,t)=comm_loc_model(:,nyears);
end
fileID = fopen('simulation_input\comm_loc_model.dat','w');
print_int2(fileID,comm_loc_model',period_trans,nmkt);

%% write distance
fileID = fopen('simulation_input\distance.dat','w');
print2(fileID,distance,nmkt,nmkt);

%% write bank_opt
bank_opt(bank_opt==99)=999;
fileID = fopen('simulation_input\bank_opt.dat','w');
print_int1(fileID,bank_opt,nmkt);

%% write pdf_ini
fileID = fopen('simulation_input\pdf_ini.dat','w');
print4(fileID,pdf_A_ini,nmkt,nz,nb_pdf,ntheta_pdf);